#!/usr/bin/env python

import subprocess
from os import getenv
from os.path import abspath

if __name__ == '__main__':
	print 'Parsing environment'
	nodeid = int(getenv('node_id'))
	protocol = getenv('protocol_id')
	inface = getenv('interface').format(node_id=nodeid)
	wsdls = getenv('wsdl_dir')
	n_serv = getenv('nr_services')
	warmup_min = getenv('warmup_min')
	warmup_max = getenv('warmup_max')
	usage = getenv('usage_per_min')
	end_wait = int(getenv('shutdown_wait'))/4
	run_time = int(getenv('experiment_duration'))*1000 - int(warmup_max)
	seed = getenv('current_rand_seed')
	classpath = getenv('classpath')	
	
	print 'Creating command to run'
	command = 'java -DnodeId={node_id} -DprotocolId={protocol_id} -Dif={interface} -DwsdlDir={wsdl_dir} -Dservices={number_services} -DwarmUpMin={warmup_min} -DwarmUpMax={warmup_max} -DusagePerMin={upm} -DendWait={endwait} -DrunTime={runtime} -Djava.net.preferIPv4Stack=true -Dseed={seed} -Dverbose=false -DpublishFile=../guest_temp/node_services{node_id}.txt -cp {classpath} no.ffi.sdtest.RunTest'.format(node_id=nodeid, protocol_id=protocol, interface=inface, wsdl_dir=wsdls, number_services=n_serv, warmup_min=warmup_min, warmup_max=warmup_max, upm=usage, endwait=end_wait, runtime=run_time, seed=seed, classpath=classpath).split()
	
	print command
	print 'Starting ASDF'
	ret = subprocess.call(command)
	print 'ASDF returned {}'.format(ret)